﻿using System;
using System.Collections.Generic;
using System.Text;

namespace RouterControl.LocalDb
{
    partial class LocalDbConnector
    {
        private void UpgradeDatabase()
        {
            var version = this.Version;
            var versionNext = (version + 1);

            try
            {
                switch (version)
                {
                    case LocalDbVersion:
                        _log.Info("Upgrade complete or not needed");

                        return; // do not change version!!

                    case 1:
                    {
                        var cmd = _db.CreateCommand();

                        cmd.CommandText = Properties.SqlQueries.v2_CreateOnlineStatus;

                        cmd.ExecuteNonQuery();

                        break;
                    }

                    default:
                        throw new Exception("unknown version: " + version);
                }

                // upgrade accomplished
                this.Version = versionNext;

                _log.Info("Local database upgraded from version " + version + " to " + versionNext);
            }
            catch (Exception ex)
            {
                _log.Fatal("Failed to upgrade local database from version " + version + " to " + versionNext, ex);

                throw;
            }

            // next upgrade round
            UpgradeDatabase();
        }
    }
}
